package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.EntScheduleBean;
import com.csweimei.rentmanagementtreasure.http.entity.WorkScheduleListBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class WorkScheduleVM extends TitleVM {
    public WorkScheduleVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("工作日程");
    }

    public ObservableField<String> companyid = new ObservableField<>();
    public ObservableField<String> keyword = new ObservableField<>();


    @Override
    public void onResume() {
        super.onResume();
        getData();
    }


    public ObservableField<Integer> showNo = new ObservableField<>();
    public ObservableList<WorkScheduleItemPVM> observableList = new ObservableArrayList<>();
    public ItemBinding<WorkScheduleItemPVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_work_schedule_p);
    private void getData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",companyid.get());
        map.put("keyword",keyword.get());
        model.workschedule(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<WorkScheduleListBean>() {
                    @Override
                    public void onNext(WorkScheduleListBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNo.set(View.GONE);
                                if (o.getData().get(0).getFList().size() > 0){
                                    if (o.getData().get(0).getFList().get(0).getHousingID() != 0){
                                        showNo.set(View.GONE);
                                        for (WorkScheduleListBean.DataDTO bean:o.getData()) {
                                            WorkScheduleItemPVM workScheduleItemPVM = new WorkScheduleItemPVM(titleVM);
                                            workScheduleItemPVM.setData(bean);
                                            observableList.add(workScheduleItemPVM);
                                        }
                                    }else {
                                        showNo.set(View.VISIBLE);
                                    }
                                }else {
                                    showNo.set(View.VISIBLE);
                                }

                            }else {
                                showNo.set(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
